package com.seu.me.thread.day3.coll013;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by feihu.Mr on 2017/11/19.
 */
public class UseQueue {

    public static void main(String[] args) throws Exception {

        //高性能无阻塞无界队列：ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String> q = new ConcurrentLinkedQueue<String>();
        q.offer("a");
        q.offer("b");
        q.offer("c");
        q.offer("d");
        q.add("e");

        System.out.println(q.poll());    //a 从头部取出元素，并从队列里删除
        System.out.println(q.size());    //4
        System.out.println(q.peek());    //b
        System.out.println(q.size());    //4


        ArrayBlockingQueue<String> array = new ArrayBlockingQueue<String>(5);
        array.put("a");
        array.put("b");
        array.add("c");
        array.add("d");
        array.add("e");
        //array.put("f");
        System.out.println(array.offer("a", 3, TimeUnit.SECONDS));


        //阻塞队列
        LinkedBlockingQueue<String> lq = new LinkedBlockingQueue<String>();
        lq.offer("a");
        lq.offer("b");
        lq.offer("c");
        lq.offer("d");
        lq.offer("e");
        lq.add("f");
        System.out.println(lq.size());

        for (Iterator iterator = lq.iterator(); iterator.hasNext(); ) {
            String string = (String) iterator.next();
            System.out.println(string);
        }

        List<String> list = new ArrayList<String>();
        System.out.println(lq.drainTo(list, 3));
        System.out.println(list.size());
        for (String string : list) {
            System.out.println(string);
        }

        final SynchronousQueue<String> sq = new SynchronousQueue<String>();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(sq.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                sq.add("seu");
            }
        });
        t2.start();
    }
}
